package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.DataSource;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.UserAreaCode;
import com.chain.BrotherExchange.utils.Androids;

import java.text.DecimalFormat;

import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyMessageVerifyCode;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyTransferPassword;

public class ExtractMoneyViewModel extends AndroidViewModel {

    private int uid = Androids.LocalConfig.getUid();
    private String token = Androids.LocalConfig.getToken();

    private MutableLiveData<String> mAccountNumberLiveData;
    private MutableLiveData<Resource<Double>> mProcedureFeeLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mSendMsmLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mExtractMoneyLiveData;

    public ExtractMoneyViewModel(@NonNull Application application) {
        super(application);
        mProcedureFeeLiveData = new MutableLiveData<>();
        mExtractMoneyLiveData = new MutableLiveData<>();
        mAccountNumberLiveData = new MutableLiveData<>();
        mSendMsmLiveData = new MutableLiveData<>();
    }

    /**
     * 获取到账数量
     *
     * @param extractMoneyNumber 提币数量
     */
    public MutableLiveData<String> accountNumber(String extractMoneyNumber) {
        if (extractMoneyNumber.length() > 0) {
            double extractMoneyNumberDouble = Double.parseDouble(extractMoneyNumber);
            Resource<Double> resource = mProcedureFeeLiveData.getValue();
            if (resource != null) {
                double procedureFee = resource.source;
                mAccountNumberLiveData.setValue(new DecimalFormat("#########.########")
                        .format(extractMoneyNumberDouble - procedureFee));
            }
        }

        return mAccountNumberLiveData;
    }

    /**
     * 提币手续费
     */
    public MutableLiveData<Resource<Double>> procedureFee() {
        DataRepository.get().procedureFee(result -> {
            if (result.isSucceed()) {
                mProcedureFeeLiveData.setValue(Resource.success(result.getData().getFee()));
            } else {
                mProcedureFeeLiveData.setValue(Resource.error(""));
            }
        });

        return mProcedureFeeLiveData;
    }

    public MutableLiveData<Resource<BaseResponseModel>> sendMessage() {
        String phone = Androids.LocalConfig.getPhone();
        mSendMsmLiveData.setValue(Resource.loading());
        getAreaCodeByMobilePhone(phone, region ->
                DataRepository.get().sendMessage(region, phone, C.API_EXTRACT_COIN_TYPE, result -> {
                    if (result.isSucceed()) {
                        mSendMsmLiveData.setValue(Resource.success(result.getMsg()));
                    } else {
                        mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
                    }
                })
        );

        return mSendMsmLiveData;
    }

    private void getAreaCodeByMobilePhone(String mobilePhone, DataSource.DataCallback<String> callback) {
        DataRepository.get().getUserAreaByMobilePhone(mobilePhone, result -> {
            UserAreaCode.DataBean resultData = result.getData();
            if (result.isSucceed() && resultData != null) {
                callback.onResult(resultData.getRegion());
            } else {
                mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
    }

    /**
     * 提币
     */
    public MutableLiveData<Resource<BaseResponseModel>> extractMoney(
            String coinId, String address, String number, String amountNumber, String gas, String code, String password) {
        if (verifyMessageVerifyCode(code) && verifyTransferPassword(password)) {
            String phone = Androids.LocalConfig.getPhone();
            mExtractMoneyLiveData.setValue(Resource.loading());
            DataRepository.get().extractMoney(uid, token, coinId, address, number, amountNumber, gas,
                    code, password, phone, result -> {
                        if (result.isSucceed()) {
                            mExtractMoneyLiveData.setValue(Resource.success(result));
                        } else {
                            mExtractMoneyLiveData.postValue(Resource.error(result.getMsg()));
                        }
                    });
        }

        return mExtractMoneyLiveData;
    }
}
